<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="./style.css">

    <style>
        * {
            padding: 0;
            margin: 0;
        }
        html,body{
            font-size:14px;
        }
        .search-list {
            height: 300px;
            border: 1px solid #ccc;
            background: #fff;
            overflow: auto;
            position: absolute;
            width: 100%;
            margin-top: 1px;
        }

        .search-list li {
            list-style: none;
            border-bottom: 1px solid #f2f2f2;
            padding: 5px 10px;
            cursor: pointer;
            display: flex;
        
        }

        .search-list li span {
            flex: 1;
            text-align: left;

        }
        .input-style{
            width:100%;
            height:30px;
            border:1px solid #f2f2f2;
            border-radius:5px;
            padding-left:10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div style="width: 400px; position: relative;margin:20px auto;">
            <input type="text" placeholder="输入编号、名称、首字母查询" v-model="filterText" @keyup="getFilterText" class="input-style" style="width: 100%;" />

            <div class="search-list" v-show="bool">
                <ul>
                    <li><span>编号</span><span>名称</span></li>
                    <li v-for="item of codeList"><span>{{item.key}}</span><span>{{item.name}}</span></li>
                </ul>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue"></script>

    <script type="text/javascript">
        // for (let item of code) {
        //     item.pinyin = pinyinUtil.getPinyin(item.name)
        // }
        var app = new Vue({
            el: '#app',
            data: function() {
                return {
                    filterText: '',
                    message: 'Hello Vue!',
                    codeList: [],
                    code: code,
                    timeStamp: 0,
                    bool: false
                }
            },
            methods: {
                getFilterText(e) {
                    this.timeStamp = e.timeStamp;
                    setTimeout(() => {
                        if (this.timeStamp == e.timeStamp) {
                            this.filterNodeList(this.filterText.toLowerCase())
                        }
                    }, 500)
                },
                filterNodeList(value) {
                    if (value) {
                        this.codeList = this.code.filter(function(item) {
                            item.pinyin = (pinyinUtil.getFirstLetter(item.name)).toLowerCase();
                            console.log(item.pinyin )
                            return item.key.indexOf(value) !== -1 || item.pinyin.indexOf(value) !== -1 || item.name.indexOf(value) !== -1;
                        })
                        this.bool = true;
                    } else {
                        this.codeList = [];
                        this.bool = false;

                    }

                }
            },
        })
    </script>
</body>

</html>